//
//  UIBarButtonItem+Button.swift
//  KLM_GN
//
//  Created by Nic on 2025/2/18.
//

import Foundation

private let buttonItemWH: CGFloat  = 30.0

extension UIBarButtonItem {
    
    static func itemWithIcon(_ icon: String, target: Any, action: Selector) -> UIBarButtonItem {
        let button = UIButton.init(type: .custom)
        let image = UIImage.init(named: icon)
        button.setImage(image, for: .normal)
        button.setImage(image, for: .highlighted)
        button.frame = CGRect.init(x: 10, y: 0, width: (image?.size.width ?? buttonItemWH) + 20, height: (image?.size.height ?? buttonItemWH) + 20)
        button.imageView?.contentMode = .right
        button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
        button.addTarget(target, action: action, for: .touchUpInside)
        let barButtonItem = UIBarButtonItem.init(customView: button)
        return barButtonItem
    }
    
    static func itemsWithIcon(_ icon: String, target: Any, action: Selector) -> [UIBarButtonItem] {
        let button = UIButton.init(type: .custom)
        let image = UIImage.init(named: icon)
        button.setImage(image, for: .normal)
        button.setImage(image, for: .highlighted)
        button.frame = CGRect.init(x: 10, y: 0, width: (image?.size.width ?? buttonItemWH) + 20, height: (image?.size.height ?? buttonItemWH) + 20)
        button.addTarget(target, action: action, for: .touchUpInside)
        let barButtonItem = UIBarButtonItem.init(customView: button)
        let nagetiveSpacer = UIBarButtonItem.init(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        nagetiveSpacer.width = -15
        return [barButtonItem, nagetiveSpacer]
    }
    
    static func itemWithBackIcon(target: Any, action: Selector) -> [UIBarButtonItem] {
        let button = UIButton.init(type: .custom)
        let image = UIImage.init(named: "Nav_back")
        button.setImage(image, for: .normal)
        button.setImage(image, for: .highlighted)
        button.contentHorizontalAlignment = .left
        let nagetiveSpacer = UIBarButtonItem.init(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        nagetiveSpacer.width = 0
        button.frame = CGRect.init(x: 0, y: 0, width: 40, height: 44)
        button.addTarget(target, action: action, for: .touchUpInside)
        let barButtonItem = UIBarButtonItem.init(customView: button)
        return [nagetiveSpacer, barButtonItem]
    }
    
    static func itemWithTitle(_ title: String, target: Any, action: Selector) -> UIBarButtonItem {
        let button = UIButton.init(type: .custom)
        button.setTitleColor(.black, for: .normal)
        button.setTitle(title, for: .normal)
        button.contentHorizontalAlignment = .right
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.frame = CGRect.init(x: 0, y: 0, width: 45, height: 30)
        button.addTarget(target, action: action, for: .touchUpInside)
        let barButtonItem = UIBarButtonItem.init(customView: button)
        return barButtonItem
    }
    
    static func itemWithCustomView(_ view: UIView, target: Any, action: Selector) -> UIBarButtonItem {
        if let control = view as? UIControl {
            control.addTarget(target, action: action, for: .touchUpInside)
        } else {
            print("视图不是 UIControl 类型，添加点击手势识别器")
            let tapGesture = UITapGestureRecognizer(target: target, action: action)
            tapGesture.cancelsTouchesInView = false
            view.addGestureRecognizer(tapGesture)
        }
        let barButtonItem = UIBarButtonItem(customView: view)
        return barButtonItem
    }
    
    static func itemWithBaseButton(_ baseBtn: KLMBaseButton, target: Any, action: Selector, configBlock:((KLMBaseButton) -> Void)? = nil) -> UIBarButtonItem {
        baseBtn.addTarget(target, action: action, for: .touchUpInside)
        let barButtonItem = UIBarButtonItem.init(customView: baseBtn)
        if let configBlock = configBlock {
            configBlock(baseBtn)
        }
        return barButtonItem
    }
}
